<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2012 HongPing All rights reserved.
// +----------------------------------------------------------------------
// | ActionName:商品报价管理
// +----------------------------------------------------------------------
// | Author: HongPing <hongping626@qq.com>
// +----------------------------------------------------------------------
class QuotesAction extends CommonAction {
	function _filter(&$map){
        //$map['id'] = array('egt',2);
	//	$map['costcode|costname'] = array('like',"%".$_POST['selectname']."%");
        
	}
    
    function _before_add(){
         $this->assign ( 'dtime', date('Y-m-d') );
    }
    
    function update(){
        //更新时新增记录
        
        $qData['prodid'] = $_POST['prodid'];
        $qData['contid'] = $_POST['contid'];
        $qData['qtype'] = $_POST['qtype'];
        $qData['qprice'] = $_POST['qprice'];
        $qData['qtaxprice'] = $_POST['qtaxprice'];
        $qData['qtime'] = strtotime($_POST['qtime']);
        $qData['createtime'] = strtotime(date('Y-m-d H:i:s'));
        $qData['updatetime'] = strtotime(date('Y-m-d H:i:s'));
        $qData['remarks'] = $_POST['remarks'];//摘要
        $qData['createuser'] = $_SESSION[C('USER_AUTH_KEY')];
        
        $setArr = array(
        'qprice'=>$qData['qprice'],
        'qtaxprice'=>$qData['qtaxprice'],
        'remarks'=>$qData['remarks'],
        'qtime'=>$qData['qtime'],
        'createuser'=>$qData['createuser'],
        'updatetime'=>strtotime(date('Y-m-d H:i:s'))
        ); 
        
        $qModel = M('Quotes');
        $where['id'] = $_POST['id'];
        $ckQprice = $qModel->where($where)->getField('qprice');
        $ckQtaxprice = $qModel->where($where)->getField('qtaxprice');
        
        
        if($qModel->where($where)->setField($setArr)){            
            //必须是有价格变动的时候才记录。
            if($ckQprice!=$qData['qprice'] or $ckQtaxprice!=$qData['qtaxprice']){
                $qLogModel = M('Quoteslog');
                $qLogModel->add($qData);
            }
            $this->success('更新成功！');            
        }else{
            $this->Error('更新失敗！');
        }
        
        
    }
    
    /*报价数据显示*/
    function index(){    
        
        if($_POST['selectname']){
            $map['prodname|partmodel|prodsize|comname|remarks'] = array('like',"%".$_POST['selectname']."%");
        }
        
        if($_POST['qtype']){
            $map['qtype'] = $_POST['qtype'];
        }
        
        $model = D ('Quotesview');//数据库视图表
        if (! empty ( $model )) {
        $this->_list ( $model, $map );
        }
        $this->display ();
    }
    
    //删除报价
    function delQuotes(){        
       $arrData = $_REQUEST['ids'];
       $countArr = count($arrData);
       
       $qModel = D('Quotes');
       $qLogModel = D('Quoteslog'); 
       $countDel = 0;
       
       for($i=0;$i<$countArr;$i++){
          $ids = '';
          unset($where);
         if($arrData[$i]){
          $ids = $arrData[$i];
          $where1['id'] = $ids;
          $where2['quoteid'] = $ids;
          $qModel->where($where1)->delete();//删除当前报价
          $qLogModel->where($where2)->delete();//删除历史报价
          $countDel +=1;
          }        
       }
       $this->success('成功删除 '.$countDel.' 个报价。');
   }
    //更新销售报价到商品预设价
    function updProductsPrice(){
       $arrData = $_REQUEST['ids'];
       $countArr = count($arrData);
       
       $qModel = D('Quotes');
       $prodModel = D('Products'); 
       $countDel = 0;
       
       for($i=0;$i<$countArr;$i++){
          $ids = '';
          unset($where);
         if($arrData[$i]){
          $ids = $arrData[$i];
          $where['id'] = $ids;
          $where['qtype'] = 1;//1为销售价
          $price = $qModel->where($where)->getField('qtaxprice');
          $pid = $qModel->where($where)->getField('prodid');  
           if($price!=0){
            $pData['presale'] = $price;
            $pData['updatetime'] = strtotime(date('Y-m-d H:i:s'));
            $pwhere['id'] = $pid;
            $rs = $prodModel->where($pwhere)->save($pData);
            $countDel +=1; 
           }
          }         
       }
       $this->success('成功更新 '.$countDel.' 个含税报价到商品预售价。');        
    }
    
    function addQuotes(){
        if($_POST['prodid_id']){$qData['prodid'] = $_POST['prodid_id'];}else{$this->error('报价商品不能为空。');exit;}
        if($_POST['contid_id']){$qData['contid'] = $_POST['contid_id'];}else{$this->error('报价单位不能为空。');exit;}
        if($_POST['contid_orgType']){$qData['qtype'] = $_POST['contid_orgType'];}else{$this->error('报价类型不能为空。');exit;}
        $qData['qprice'] = $_POST['qprice'];
        $qData['qtaxprice'] = $_POST['qtaxprice'];
        if($_POST['qtime']){$qData['qtime'] = strtotime($_POST['qtime']);}else{$this->error('报价日期不能为空。');exit;}//日期
        $qData['createtime'] = strtotime(date('Y-m-d H:i:s'));
        $qData['updatetime'] = strtotime(date('Y-m-d H:i:s'));
        $qData['remarks'] = $_POST['remarks'];//摘要
        $qData['createuser'] = $_SESSION[C('USER_AUTH_KEY')];
        
        $qModel = M('Quotes');
        $qLogModel = M('Quoteslog');
        
        $qMap['prodid'] = $qData['prodid'];
        $qMap['contid'] = $qData['contid'];
        $qMap['qtype'] = $qData['qtype'];
        
        $checkQ = $qModel->where($qMap)->getField('id');
        if($checkQ){$this->error('发现报价中已存在相同报价，即商品、报价单位和类型一致。');exit;}
        $qResult = $qModel->add($qData);
        $qLogModel->add($qData);
       // $sql = $qModel->getLastSql();
        if($qResult){
            $this->success('新增报价成功。');
        }else{
            $this->error('新增报价失败。');
        }
        
    }
    
  function showlog(){
    $id = $_REQUEST['id'];
    $listM = M('Quotes');
    $listMlog = M('Quoteslog');
    $map['id'] = $id;
    $ckProdid = $listM->where($map)->getField('prodid');
    $ckContid = $listM->where($map)->getField('contid');
    $ckQtype = $listM->where($map)->getField('qtype');
    $map2['prodid'] = $ckProdid;
    $map2['contid'] = $ckContid;
    $map2['qtype'] = $ckQtype;
    //dump($listCk);
    $list = $listMlog->where($map2)->order('createtime desc')->select();
    $this->assign('list',$list);
    //echo $listMlog->getLastSql();
    $this->display();
  }
  
  function showContrast(){
    $pid = $_REQUEST['pid'];
    $listM = M('Quotes');
    $mapA['prodid'] = $pid;
    $mapB['prodid'] = $pid;
    $mapA['qtype'] = 1;//销售价
    $mapB['qtype'] = 2;//进货价
    $listA = $listM->where($mapA)->order('qprice desc')->select();//销售价最高
    $listB = $listM->where($mapB)->order('qprice')->select();//进货价最低
    $this->assign('listA',$listA);
    $this->assign('listB',$listB);
    //echo $listM->getLastSql();
    $this->display();
  }
   
 
}
?>